package com.semidata.trp.algorithm.compile;

import com.semidata.rtd.core.log.Logger;
import com.semidata.rtd.engine.dynamic.DynamicClassLoader;
import com.semidata.rtd.engine.dynamic.DynamicCompiler;
import com.semidata.rtd.engine.job.Job;
import com.semidata.rtd.engine.job.SyncTask;
import com.semidata.trp.algorithm.job.AlgorithmJob;
import com.semidata.trp.algorithm.job.AlgorithmJobContext;

public class TaskCompile extends SyncTask{

	public TaskCompile(Job job) {
		super(job, 0);
	}

	@Override
	public void run() throws Exception {
		System.out.println("compile");
		AlgorithmJobContext context = ((AlgorithmJob)getJob()).getJobContext();
		DynamicClassLoader loader = new DynamicClassLoader();
		try {
			Class<?> clazz = DynamicCompiler
			        .compile(loader, "com.semidata.trp.algorithm.Algorithm", context.getCode());
			if (clazz == null) {
				throw new Exception(DynamicCompiler.message);
			}
			context.setAlgorithm(clazz);
		} catch (Exception e) {
			Logger.log("ERROR", "compile fail : " + e.getMessage());
			throw e;
		}
		
	}

}
